#include "TextEntity.h"
#include <TGS/GeoMath2.0/Envelope.h>
#include <TGS/MapControl2.0/RenderContext.h>
#include <QPainter>


TextEntity::TextEntity(void)
{
}


TextEntity::~TextEntity(void)
{
}

void TextEntity::setText(const QString& text, const Point& pos, double height, double angle)
{
	_text = text;
	_position = pos;
	_height = height;
	_angle = angle;
}

TGS::GeoMath::Envelope TextEntity::getEnvelope() const
{
	if(!isSpaceModel()) return Envelope();

	return Envelope(_position, _position);
}

void TextEntity::render( RenderContext* renderContext )
{
	renderContext->painter()->save();

	QPen pen(QBrush(getColor()), getWidth());
	renderContext->painter()->setPen(pen);

	QPoint position = renderContext->mapToPixel().mapToScreen(_position);

	int pixHeight = _height / renderContext->mapToPixel().mapUnitsPerPixel();
	if( 0 == pixHeight) pixHeight = 1;
	pixHeight *= 1.5;

	QFont font = renderContext->painter()->font();
	font.setPixelSize(pixHeight);
	renderContext->painter()->setFont(font);

	renderContext->painter()->translate(position);
	renderContext->painter()->rotate(-_angle * 180 / 3.1415926);
	renderContext->painter()->translate(-position);	
	renderContext->painter()->drawText(position, _text);

	renderContext->painter()->restore();
}

void TextEntity::traslate( const Point& point )
{
	_position.setX(_position.getX() + point.getX());
	_position.setY(_position.getY() + point.getY());
}
